//index.js
//获取应用实例
var api = require("../../api.js"),
  r = "",
  app = getApp();

Page({
  data: {
    tabbar: {},
    pageIndex: 1,
    dataList: [], //数据源
    windowWidth: 0, //页面视图宽度
    windowHeight: 0, //视图高度
    imgMargin: 6, //图片边距: 单位px
    imgWidth: 0, //图片宽度: 单位px
    topArr: [0, 0], //存储每列的累积top    
  },

  onLoad: function(options) {
    r = this;
    app.editTabbar();
    if (options.shareType) {
      app.globalData.shareType = options.shareType;
      app.globalData.userId = options.id;
    }
    app.globalData.shareType = '';
    app.globalData.userId = '';
    if (options.id) {
      setTimeout(() => {
        wx.navigateTo({
          url: `../audiodetail/audiodetail?id=${options.id}`
        });
      }, 800);
    };
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        var windowWidth = res.windowWidth;
        var imgMargin = that.data.imgMargin;
        //两列，每列的图片宽度
        var imgWidth = (windowWidth - imgMargin * 3) / 2;
        that.setData({
          windowWidth: windowWidth,
          windowHeight: res.windowHeight,
          imgWidth: imgWidth
        }, function() {
          that.loadMoreImages(); //初始化数据
        });
      },
    })
  },
  //加载图片
  loadImage: function(e) {
    var index = e.currentTarget.dataset.index; //图片所在索引
    var imgW = e.detail.width,
      imgH = e.detail.height; //图片实际宽度和高度
    var imgWidth = this.data.imgWidth; //图片宽度
    var imgScaleH = imgWidth / imgW * imgH; //计算图片应该显示的高度
    var dataList = this.data.dataList;
    var margin = this.data.imgMargin; //图片间距
    //第一列的累积top，和第二列的累积top
    var firtColH = this.data.topArr[0],
      secondColH = this.data.topArr[1];
    var obj = dataList[index];
    // console.log(obj)
    obj.height = imgScaleH;
    if (firtColH < secondColH) { //表示新图片应该放到第一列
      obj.left = margin;
      obj.top = firtColH + margin;
      firtColH += margin + obj.height;
    } else { //放到第二列
      obj.left = margin * 2 + imgWidth;
      obj.top = secondColH + margin;
      secondColH += margin + obj.height;
    }
    this.setData({
      dataList: dataList,
      topArr: [firtColH, secondColH],
    });
  },
  //加载更多图片
  loadMoreImages: function() {
    const that = this;
    const tmpArr = [];
    app.getData.tt_expertes(that.data.pageIndex, 10).then(res => {
      console.log(res)
      const arr = res.data;
      if (arr.length > 0) {
        arr.forEach(item => {
          var obj = {
            outImg: item.headImg,
            height: 0,top: 0,left: 0,
            tea_open_id: item.openId,
            copName: item.name,
            /** 标签文字 */
            tag: item.tag,
            outDesc: item.introduction
          };
          tmpArr.push(obj)
        });
        that.data.pageIndex++;
        that.setData({
          dataList: that.data.dataList.concat(tmpArr),
          loading: true
        });
        setTimeout(() => {
          wx.hideLoading()
        }, 500)
      }
    }).catch(res => {

    })

  },
  /**预览图片 */
  // previewImg: function (e) {
  // const index = e.currentTarget.dataset.index;
  // const dataList = this.data.dataList;
  // const currentSrc = dataList[index].src;
  // wx.previewImage({
  //  urls: [currentSrc],
  // });
  // },

  /** 跳转详情 */
  godetail: function(e) {
    const tea_open_id = e.currentTarget.dataset.id;
    wx.navigateTo({ url: "../audiodetail/audiodetail?tea_open_id=" + tea_open_id  });
  },
  
  onPullDownRefresh: function() {
    var that = this;
    wx.showNavigationBarLoading(), 
    that.setData({
      dataList: [],
      pageIndex: 1
    }), that.loadMoreImages();
    setTimeout(function() {
      wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
    }, 1500);
  },
  /** 下拉触底事件 */
  onReachBottom: function() {
    wx.showNavigationBarLoading();
    var that = this; var tmpArr = [];
    if (that.data.loading) {
      /** 请求智库专家数据 */
      wx.request({
        url: api.audio.tt_expertes,
        data: { page_index: that.data.pageIndex },
        success: function(res) {
          if (res.data.code == 200) {
            var n = res.data.data;
            if (n.length != 0) {
              for (let i = 0; i < res.data.data.length; i++) {
                var obj = {
                  outImg: res.data.data[i].headImg,
                  height: 0, top: 0, left: 0,
                  tea_open_id: res.data.data[i].openId,
                  copName: res.data.data[i].name,
                  /** 标签文字 */
                  tag: res.data.data[i].tag,
                  outDesc: res.data.data[i].introduction
                };
                tmpArr.push(obj);
              };
              var dataList = that.data.dataList.concat(tmpArr);
              that.data.pageIndex++;
              that.setData({
                dataList: dataList,
                loading: true
              }, function() {
                wx.hideLoading()
              })
            } else {
              that.setData({
                loading: false
              })
            }
          }
        }
      });
    }
    setTimeout(function() {
      wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
    }, 1500);
  },
})